<template>
	<el-card class="layout-query">
		<el-form ref="queryParamsRef" :inline="true">
			<el-form-item prop="roleName" label="角色名称">
				<el-input v-model="state.queryForm.roleName" placeholder="角色名称"></el-input>
			</el-form-item>
			<el-form-item prop="roleCode" label="角色编码">
				<el-input v-model="state.queryForm.roleCode" placeholder="角色编码"></el-input>
			</el-form-item>

			<el-form-item>
				<el-button icon="Search" type="primary" @click="handleSearch">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="handleReset">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>

	<el-card>
		<el-space style="width: 100%; justify-content: flex-end">
			<el-button icon="Plus" type="primary" @click="updateRoleHandle(true, null)">新增</el-button>
		</el-space>

		<el-table v-loading="state.dataListLoading" :data="state.dataList" border class="layout-table" @selection-change="selectionChangeHandle">
			<el-table-column prop="id" label="序号" header-align="center" align="center"></el-table-column>
			<el-table-column prop="roleName" label="角色名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="roleCode" label="角色编码" header-align="center" align="center"></el-table-column>
			<el-table-column prop="roleDesc" label="角色说明" header-align="center" align="center"></el-table-column>
			<!-- <el-table-column prop="roleType" label="角色类型" header-align="center" align="center"></el-table-column> -->
			<el-table-column prop="createTime" label="创建时间" header-align="center" align="center" width="180"></el-table-column>
			<el-table-column prop="iflag" label="状态" header-align="center" align="center">
				<template #default="scope">
					<span v-if="scope.row.iflag">正常</span>
					<span v-else>禁止</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
				<template #default="scope">
					<el-button type="primary" link @click="updateRoleHandle(false, scope.row)">修改</el-button>
					<el-button type="primary" link @click="updatePermHandle(false, scope.row)">权限配置</el-button>
					<el-button type="primary" link @click="deleteRecord(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="state.queryForm.pageIndex"
			:page-size="state.queryForm.pageSize"
			:total="state.total"
			layout="total, sizes, prev, pager, next, jumper"
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
		>
		</el-pagination>

		<!-- 新增/修改 -->
		<updateRoleDialog ref="updateRoleRef" @submit-success="getRoleList"></updateRoleDialog>
		<!-- 权限修改 -->
		<updatePermSetDialog ref="updatePermRef" @refresh-data-list="getRoleList"></updatePermSetDialog>
	</el-card>
</template>

<script setup lang="ts" name="SysRoleIndex">
import { reactive, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import updatePermSetDialog from './components/updatePermSetDialog.vue'
import updateRoleDialog from './components/updateRoleDialog.vue'

import { queryRoleApi } from '@/api/system/rbac'
// import { lstat } from 'fs'

// interface IListDataType { // 列表数据项
//     id: Number
// 	roleName: string
// 	roleCode:string
// 	roleDesc: string
// 	roleType: string
// 	createTime: string
// 	iflag: Number
// }
// lstat dataList = ref<IListDataType[]>([]) // 列表数据

const state = reactive({
	queryForm: {
		roleName: '',
		roleCode: '',
		pageIndex: 1,
		pageSize: 10
	},
	total: 1,
	dataListLoading: false,
	dataList: []
})

const showUpdateRoleDialog = ref<boolean>(false)
const showUpdatePermDialog = ref<boolean>(false)

const reset = () => {
	state.queryForm.roleName = ''
	state.queryForm.roleCode = ''
	state.queryForm.pageIndex = 1
	state.queryForm.pageSize = 10
}

const selectionChangeHandle = () => {}

// role操作
const updateRoleRef = ref()
const updateRoleHandle = (isUpdate: Boolean, row: any) => {
	// true 新增  false 修改
	updateRoleRef.value.init(isUpdate, row)
}

// 权限操作
const updatePermRef = ref()
const updatePermHandle = (isUpdate: Boolean, row: any) => {
	updatePermRef.value.init(isUpdate, row)
}

const deleteRecord = (row: any) => {
	ElMessageBox.confirm('是否删除?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			ElMessage({
				type: 'success',
				message: '删除成功'
			})
		})
		.catch(() => {
			ElMessage({
				type: 'info',
				message: '删除失败'
			})
		})
}

// 列表数据获取
const getRoleList = async () => {
	state.dataListLoading = true
	await queryRoleApi(state.queryForm).then(res => {
		// console.log(res.data.list)
		state.dataListLoading = false
		state.dataList = res.data.list
	})
}
const handleCurrentChange = (val: number) => {
	state.queryForm.pageIndex = val
	getRoleList()
}
const handleSizeChange = (val: number) => {
	state.queryForm.pageSize = val
	getRoleList()
}

const handleSearch = () => {
	console.log('搜索参数:', state.queryForm)
	getRoleList()
}

const handleReset = () => {
	reset()
	getRoleList()
}

onMounted(() => {
	getRoleList()
})
</script>

<style scoped>
.el-dropdown-link {
	display: flex;
	align-items: center;
	line-height: normal;
	margin-left: 10px;
}
</style>
